package com.example.wanmengnovel;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.*;

@WebServlet("/novel-enroll")
public class Enroll extends HttpServlet {
    public Connection getConnection() throws SQLException, ClassNotFoundException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        System.out.println("JDBC驱动加载成功！");
        String url = "jdbc:mysql://localhost:3306/users";
        String user = "root";
        String password = "lcw0909.";
        Connection connection = DriverManager.getConnection(url, user, password);
        return connection;
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String userAccount = req.getParameter("userAccount");
        String userEmail = req.getParameter("userEmail");
        String userPassword = req.getParameter("userPassword");
        String rPassword = req.getParameter("RPassword");
        //获取网页的用户输入

        if (!userPassword.equals(rPassword)) {
            resp.setContentType("text/html;charset=utf-8");
            resp.getWriter().print("<script language='javascript'>" +
                    "alert('两次密码输入有误！');" +
                    "window.location.href='enroll.jsp';</script>')");
        }
        //如果两次密码错误则直接返回

        if (username.length() > 6) {
            resp.setContentType("text/html;charset=utf-8");
            resp.getWriter().print("<script language='javascript'>" +
                    "alert('用户名太长了，请不要超过 6 个字符！');" +
                    "window.location.href='enroll.jsp';</script>')");
        }
        //如果用户名过长了则返回错误

        if (userAccount.length() < 3 || userAccount.length() > 10) {
            resp.setContentType("text/html;charset=utf-8");
            resp.getWriter().print("<script language='javascript'>" +
                    "alert('账号输入有误，请输入 3 ~ 10 位 的数字！');" +
                    "window.location.href='enroll.jsp';</script>')");
        }
        //如果账号输入有误则返回错误


        int mun = 0;
        try {
            System.out.println(userAccount);
            mun =Integer.valueOf(userAccount);
            // 如果没抛出异常，则 date 可以正确转换成 int
        }catch(NumberFormatException e){
            resp.setContentType("text/html;charset=utf-8");
            resp.getWriter().print("<script language='javascript'>" +
                    "alert('账号输入有误，请输入数字！');" +
                    "window.location.href='enroll.jsp';</script>')");
        }
        //如果用户输入的账号不是纯数字，则采取异常的形式抛出

        try {
            Connection connection = getConnection();
            String sql = "INSERT INTO novelusers (userName,userEmail,userAccount,userPassword) VALUES (?,?,?,?);";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, username);
            preparedStatement.setString(2, userEmail);
            preparedStatement.setInt(3, mun);
            preparedStatement.setString(4, userPassword);

            int rowsInserted = preparedStatement.executeUpdate();
            if (rowsInserted > 0) {
                System.out.println("数据添加成功！");
                resp.setContentType("text/html;charset=utf-8");
                resp.getWriter().print("<script language='javascript'>" +
                        "alert('注册成功！');" +
                        "window.location.href='index.jsp';</script>')");
            }
            preparedStatement.close();
            connection.close();
        } catch (SQLIntegrityConstraintViolationException e){
            resp.setContentType("text/html;charset=utf-8");
            resp.getWriter().print("<script language='javascript'>" +
                    "alert('账号已经存在！');" +
                    "window.location.href='index.jsp';</script>')");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }


    }
}
